From 021f70afac9ee35031f6607af8c8eef968d6ca38 Mon Sep 17 00:00:00 2001 From: "Mark J. Berger" Date: Fri, 5 Sep 2014 08:23:57 -0700 Subject: [PATCH] Create hg repo with --hg flag on cargo new --- src/bin/new.rs | 5 ++++- src/cargo/ops/cargo_new.rs | 23 ++++++++++++++--------- src/cargo/util/mod.rs | 2 ++ src/cargo/util/vcs.rs | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 src/cargo/util/vcs.rs diff --git a/src/bin/new.rs b/src/bin/new.rs index 7511746c0..86ce33fdc 100644 --- a/src/bin/new.rs +++ b/src/bin/new.rs @@ -17,6 +17,7 @@ Options: --no-git Don't initialize a new git repository --git Initialize a new git repository, overriding a global `git = false` configuration + --hg Initialize a new hg repository --travis Create a .travis.yml file --bin Use a binary instead of a library template -v, --verbose Use verbose output @@ -26,11 +27,13 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult debug!("executing; cmd=cargo-new; args={}", os::args()); shell.set_verbose(options.flag_verbose); - let Options { flag_no_git, flag_travis, flag_bin, arg_path, flag_git, .. } = options; + let Options { flag_no_git, flag_travis, + flag_bin,arg_path, flag_git, flag_hg, .. } = options; let opts = ops::NewOptions { no_git: flag_no_git, git: flag_git, + hg: flag_hg, travis: flag_travis, path: arg_path.as_slice(), bin: flag_bin, diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index d74e381e1..edbb4c3f3 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -1,14 +1,15 @@ use std::os; use std::io::{mod, fs, File}; -use git2::{Repository, Config}; +use git2::Config; -use util::{CargoResult, human, ChainError, config, internal}; +use util::{GitRepo, HgRepo, CargoResult, human, ChainError, config, internal}; use core::shell::MultiShell; pub struct NewOptions<'a> { pub no_git: bool, pub git: bool, + pub hg: bool, pub travis: bool, pub bin: bool, pub path: &'a str, @@ -35,15 +36,19 @@ pub fn new(opts: NewOptions, _shell: &mut MultiShell) -> CargoResult<()> { fn mk(path: &Path, name: &str, opts: &NewOptions) -> CargoResult<()> { let cfg = try!(global_config()); - if !opts.git && (opts.no_git || cfg.git == Some(false)) { + let mut ignore = "/target\n".to_string(); + if !opts.bin { + ignore.push_str("/Cargo.lock\n"); + } + + if opts.hg { + try!(HgRepo::init(path)); + try!(File::create(&path.join(".hgignore")).write(ignore.as_bytes())); + } else if !opts.git && (opts.no_git || cfg.git == Some(false)) { try!(fs::mkdir(path, io::UserRWX)); } else { - try!(Repository::init(path)); - let mut gitignore = "/target\n".to_string(); - if !opts.bin { - gitignore.push_str("/Cargo.lock\n"); - } - try!(File::create(&path.join(".gitignore")).write(gitignore.as_bytes())); + try!(GitRepo::init(path)); + try!(File::create(&path.join(".gitignore")).write(ignore.as_bytes())); } let (author_name, email) = try!(discover_author()); diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index 6b1aa72b2..33bc04269 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -11,6 +11,7 @@ pub use self::dependency_queue::{DependencyQueue, Fresh, Dirty, Freshness}; pub use self::dependency_queue::Dependency; pub use self::graph::Graph; pub use self::to_url::ToUrl; +pub use self::vcs::{GitRepo, HgRepo}; pub mod graph; pub mod process_builder; @@ -25,3 +26,4 @@ pub mod profile; mod pool; mod dependency_queue; mod to_url; +pub mod vcs; diff --git a/src/cargo/util/vcs.rs b/src/cargo/util/vcs.rs new file mode 100644 index 000000000..04b2fb376 --- /dev/null +++ b/src/cargo/util/vcs.rs @@ -0,0 +1,21 @@ +use git2; + +use util::{CargoResult, process}; + +pub struct HgRepo; +pub struct GitRepo; + +impl GitRepo { + pub fn init(path: &Path) -> CargoResult { + try!(git2::Repository::init(path)); + return Ok(GitRepo) + } +} + +impl HgRepo { + pub fn init(path: &Path) -> CargoResult { + let path_str = path.as_str().unwrap(); + try!(process("hg").arg("init").arg(path_str).exec()); + return Ok(HgRepo) + } +} -- 2.30.2